package com.yellowpage.ndc.service;

import java.util.List;
import java.util.Map;

import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

import com.yellowpage.ndc.data.Customer;
import com.yellowpage.ndc.dao.CustomerDao;

public class CustomerServiceImpl extends BaseServiceImpl implements
		CustomerService {
	private CustomerDao customerDao;

	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}

	/* Smooth generate Method */
	public Customer getCustomer(Long id) {
		return customerDao.get(id);
	}
	
	public Customer getCustomerByCustId(String custId) {
		return customerDao.getCustomerByCustId(custId);
	}

	public List<Customer> getAllCustomer() {
		return customerDao.getAll();
	}

	public List<Map> queryCustomerByPage(Map queryMap, PageRender pageRender) {

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(
						" SELECT a.CUST_ID , a.GROUP_ID , a.NAME1 , a.NAME2 , a.ADDR_ID , a.MAIN_LISTING_ID , a.MAIN_LISTING_STATUS , a.GRADE , a.NONPUBLIC , a.CUST_STATUS , a.NOTE , "
								+ "       a.MODIFY_TIME , a.MODIFY_EMP_ID , a.CUST_TYPE_CODE , a.CLASS_MAIN_ID , a.ORG_ID , a.URL , a.WEIBO , a.WEIXIN , a.YIXIN , a.LOGO , a.SAFE_STATUS , a.BX_NAME , a.PY_NAME , a.ENG_NAME,    "
								+ "       (select ad.addr from db_ndc.ADDRESS ad where a.addr_id = ad.addr_id) as addr, "
								+ "    (select l.tel from db_ndc.LISTING l where a.main_listing_id = l.list_id) as tel "
								+ "  FROM db_ndc.CUSTOMER a WHERE 1 = 1 ")
				.addCondition(" and CUST_ID = :custId ")
				.addCondition(" and GROUP_ID = :groupId ")
				.addCondition(" and NAME1 = :name1 ")
				.addCondition(" and NAME2 = :name2 ")
				.addCondition(" and MAIN_LISTING_STATUS = :mainListingStatus ")
				.addCondition(" and GRADE = :grade ")
				.addCondition(" and NONPUBLIC = :nonpublic ")
				.addCondition(" and CUST_STATUS = :custStatus ")
				.addCondition(" and NOTE = :note ")
				.addCondition(" and date_format(MODIFY_TIME,'%Y-%m-%d') =  :modifyTime  ")
				.addCondition(" and MODIFY_EMP_ID = :modifyEmpId ")
				.addCondition(" and CUST_TYPE_CODE = :custTypeCode ")
				.addCondition(" and CLASS_MAIN_ID = :classMainId ")
				.addCondition(" and ORG_ID = :orgId ")
				.addCondition(" and URL = :url ")
				.addCondition(" and WEIBO = :weibo ")
				.addCondition(" and WEIXIN = :weixin ")
				.addCondition(" and YIXIN = :yixin ")
				.addCondition(" and LOGO = :logo ")
				.addCondition(" and SAFE_STATUS = :safeStatus ")
				.addCondition(" and BX_NAME = :bxName ")
				.addCondition(" and PY_NAME = :pyName ")
				.addCondition(" and ENG_NAME = :engName ")
				.addCondition(" and MAIN_LISTING_ID in (select l.list_id from db_ndc.LISTING l where 1 = 1 and l.tel = : tel) ")
				.addCondition(" and ADDR_ID in (select ad.addr_id from db_ndc.ADDRESS ad where 1 = 1 and ad.addr = : addr) ");

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}

	public void saveCustomer(Customer customer) {
		customerDao.save(customer);
	}

	public void updateCustomer(Customer customer) {
		customerDao.update(customer);
	}

	public void delete(List deleteList) {
		customerDao.remove(deleteList);
	}

}